package com.bjyhl.medical.main.recommend;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.core.bean.ItemEntity;
import com.bjyhl.ui.recycler.DataConverter;
import com.bjyhl.ui.recycler.ItemType;
import com.bjyhl.ui.recycler.MultipleFields;
import com.bjyhl.ui.recycler.MultipleItemEntity;
import com.bjyhl.ui.recycler.MultipleRecyclerAdapter;
import com.blankj.utilcode.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * banners，banners_inline, image_text_list, info_group
 */
public class RecommendDataConverter extends DataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        final int size = dataArray.size();
        for(int i=0; i<size; i++){
            JSONObject data = dataArray.getJSONObject(i);

            String name = data.getString("name");
            String code = data.getString("code");
            List<ItemEntity> itemEntities = null;
            int itemType = 0;
            int spanSize = MultipleRecyclerAdapter.SPAN_SIZE_TOTAL;

            /**
             * name 单独保存
             */
            if(!StringUtils.isEmpty(name)) {
                ENTITIES.add(MultipleItemEntity.builder()
                        .setField(MultipleFields.ITEM_TYPE, ItemType.TEXT)
                        .setField(MultipleFields.SPAN_SIZE, spanSize)
                        .setField(MultipleFields.TEXT, name)
                        .build());
            }

            // 分组
            JSONArray itemGroupArray = data.getJSONArray("items");
            // 分组下items列表
            itemType = ItemType.BANNER;
            itemEntities = null;
            for (int j=0; j<itemGroupArray.size(); j++) {
                JSONObject itemGroup = itemGroupArray.getJSONObject(j);
                String layout = itemGroup.getString("layout");
                JSONArray itemArray = itemGroup.getJSONArray("items");
                switch (layout) {

                    /**
                     * banner, service, item9按list保存
                     */
                    case "banner":
                        itemType = ItemType.BANNER;
                        break;
                    case "service":
                        itemType = ItemType.SERVICE;
                        break;
                    case "square-9":
                    case "rectangle-9":
                        itemType = ItemType.ITEM_9;
                        break;

                    /**
                     * item4, item5按item保存
                     */
                    case "square-5":
                    case "rectangle-5":
                        itemType = ItemType.ITEM;
                        spanSize = MultipleRecyclerAdapter.SPAN_SIZE_TOTAL / 5;
                        break;
                    case "square-4":
                    case "rectangle-4":
                        itemType = ItemType.ITEM;
                        spanSize = MultipleRecyclerAdapter.SPAN_SIZE_TOTAL / 4;
                        break;
                    default:
                        break;
                }

                if(itemArray.size() > 0) {
                    for (int k=0; k<itemArray.size(); k++) {
                        JSONObject item = itemArray.getJSONObject(k);
                        ItemEntity itemEntity = new ItemEntity();
                        itemEntity.setId(item.getInteger("id"));
                        itemEntity.setModule(item.getString("module"));
                        itemEntity.setType(item.getString("type"));
                        itemEntity.setDataType(item.getString("dataType"));
                        itemEntity.setTitle(item.getString("title"));
                        itemEntity.setCover(item.getString("cover"));
                        itemEntity.setSeed(item.getString("seed"));
                        itemEntity.setSubtitle(item.getString("subtitle"));
                        itemEntity.setLink(item.getString("link"));
                        itemEntity.setTag(item.getString("tag"));
                        itemEntity.setBackground(item.getString("background"));
                        itemEntity.setCopyRightsId(item.getInteger("copyRightsId"));

                        int _itemType = itemType;
                        int _spanSize = spanSize;

                        if (itemType == ItemType.ITEM) {
                            ENTITIES.add(MultipleItemEntity.builder()
                                    .setField(MultipleFields.ITEM_TYPE, _itemType)
                                    .setField(MultipleFields.SPAN_SIZE, _spanSize)
                                    .setField(MultipleFields.CODE, code)
                                    .setField(MultipleFields.ITEM, itemEntity)
                                    .build());
                        } else {
                            if(itemEntities == null) itemEntities = new ArrayList<>();
                            itemEntities.add(itemEntity);
                        }
                    }

                    if(itemType != ItemType.ITEM) {
                        ENTITIES.add(MultipleItemEntity.builder()
                                .setField(MultipleFields.ITEM_TYPE, itemType)
                                .setField(MultipleFields.SPAN_SIZE, spanSize)
                                .setField(MultipleFields.CODE, code)
                                .setField(MultipleFields.ITEMS, itemEntities)
                                .build());
                    }

                }
            }



        }
        return ENTITIES;
    }
}
